package com.strategy.entity;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;
import lombok.experimental.Accessors;

@Data
@Accessors(chain = true)
public class ResultResponse<T>{
    private int code;
    private String msg;
    private T data;
    private Long timestamp = System.currentTimeMillis();

    // 分页数据专用字段
    private Long total;
    private Long current;
    private Long pageSize;

    public static <T> ResultResponse<T> success(T data) {
        ResultResponse<T> response = new ResultResponse<>();
        response.setCode(200).setMsg("success");
        response.setData(data);

        // 自动处理分页数据
        if (data instanceof IPage<?> page) {
            response.setTotal(page.getTotal())
                    .setCurrent(page.getCurrent())
                    .setPageSize(page.getSize());
        }
        return response;
    }

    public static <T> ResultResponse<T> error(int code, String message) {
        ResultResponse<T> response = new ResultResponse<>();
        response.setCode(code).setMsg(message);
        return response;
    }

    public static ResultResponse<Void> success() {
        return success(null);
    }

}
